<?php
	require 'functions.php';
	spl_autoload_register('generic_autoload');
	try 
	{
		Controller_Template::$db = new EKADBPDO(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PWD);
	}
	catch(PDOException $e)
	{
		//error message here
		echo $e;	
	}
	preg_match('#^'.$_SERVER['HTTP_ROOT'].'(\w+)?#', $_SERVER['REQUEST_URI'], $match);
	if (empty($match[1]))
	{
		$controller = Controller_Index::getInstance();
		$controller->index();
	}
	else
	{
		if (isset($match[1]))
		{
			switch ($match[1])
			{
				case 'procedure':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'procedure/(\w+)?#', $_SERVER['REQUEST_URI'], $match2))
					{
						$controller = Controller_Procedure::getInstance();
						if (empty($match2[1]))
						{
							clearAllCookies();
							session_start();
							if (isset($_SESSION['selectedProcId']))
							{
								$controller->selectedProcProcedure($_SESSION['selectedProcId']);
							}
							else 
							{
								$controller->index();
							}
							session_write_close();
						}
						if (isset($match2[1]))
						{
							switch ($match2[1])
							{
								case 'selected':
									session_start();
									if (isset($_SESSION['selectedProcId']))
									{
										$controller->selectedProcProcedure($_SESSION['selectedProcId']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
							}
						}
					}
					break;
				case 'supplier':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'supplier#', $_SERVER['REQUEST_URI'], $match))
					{
						clearAllCookies();
						$controller = Controller_SupplierContacts::getInstance();
						$controller->index();
					}
					break;
				case 'inventory':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'inventory#', $_SERVER['REQUEST_URI'], $match))
					{
						clearAllCookies();
						$controller = Controller_Inventory::getInstance();
						$controller->index();
					}
					break;
				case 'help':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'help#', $_SERVER['REQUEST_URI'], $match))
					{
						clearAllCookies();
						$controller = Controller_Help::getInstance();
						$controller->index();
					}
					break;
				case 'appointment':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'appointment/(\w+)?#', $_SERVER['REQUEST_URI'], $match2))
					{
						$controller = Controller_Appointment::getInstance();
						if (empty($match2[1]))
						{
							clearAllCookies();
							session_start();
							if (isset($_SESSION['searchProfilePhone']) and empty($_SESSION['date']) and isset($_SESSION['lastURI']))
							{
								$controller->search($_SESSION['searchProfilePhone'], date("Y-m-d"), $_SESSION['lastURI']);
							}
							elseif (isset($_SESSION['client_id']) and isset($_SESSION['date']) and isset($_SESSION['appointmentId']))
							{
								$controller->create($_SESSION['client_id'], $_SESSION['date'], $_SESSION['appointmentId']);
							}
							elseif (isset($_SESSION['selectedCarIndex']) and isset($_SESSION['client_phone']) and isset($_SESSION['date']))
							{
								$controller->selectCars($_SESSION['selectedCarIndex'], $_SESSION['client_phone'], $_SESSION['date']);
							}
							elseif (isset($_SESSION['appointmentId']) and isset($_SESSION['date']))
							{
								$controller->selectAppointments($_SESSION['appointmentId'], $_SESSION['date']);
							}
							elseif (isset($_SESSION['searchProfilePhone']) and isset($_SESSION['date']) and isset($_SESSION['lastURI']))
							{
								$controller->search($_SESSION['searchProfilePhone'], $_SESSION['date'], $_SESSION['lastURI']);
							}
							elseif (isset($_SESSION['date']))
							{
								$controller->displayWeeklyAppointments($_SESSION['date']);
							}
							else 
							{
								$controller->index();
							}
							session_write_close();
						}
						if (isset($match2[1]))
						{
							switch ($match2[1])
							{
								case 'selected':
									session_start();
									if (isset($_SESSION['appointmentId']) and isset($_SESSION['date']))
									{
										$controller->selectAppointments($_SESSION['appointmentId'], $_SESSION['date']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'search':
									session_start();
									if (isset($_SESSION['searchProfilePhone']) and isset($_SESSION['date']) and isset($_SESSION['lastURI']))
									{
										$controller->search($_SESSION['searchProfilePhone'], $_SESSION['date'], $_SESSION['lastURI']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'create':
									session_start();
									if (isset($_SESSION['client_id']) and isset($_SESSION['date']) and isset($_SESSION['appointmentId']))
									{
										$controller->create($_SESSION['client_id'], $_SESSION['date'], $_SESSION['appointmentId']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'car':
									session_start();
									if (isset($_SESSION['selectedCarIndex']) and isset($_SESSION['client_phone']) and isset($_SESSION['date']))
									{
										$controller->selectCars($_SESSION['selectedCarIndex'], $_SESSION['client_phone'], $_SESSION['date']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'week':
									session_start();
									if (isset($_SESSION['date']))
									{
										$controller->displayWeeklyAppointments($_SESSION['date']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
							}
						}
					}
					break;
				case 'notification':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'notification/(\w+)?#', $_SERVER['REQUEST_URI'], $match2))
					{
						$controller = Controller_Notification::getInstance();
						if (empty($match2[1]))
						{
							clearAllCookies();
							session_start();
							if (isset($_SESSION['notification_selected']))
							{
								$controller->update($_SESSION['notification_selected']);
							}
							else 
							{
								$controller->index();
							}
							session_write_close();
						}
						if (isset($match2[1]))
						{
							switch ($match2[1])
							{
								case 'edited':
									session_start();
									if (isset($_SESSION['notification_selected']))
									{
										$controller->update($_SESSION['notification_selected']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'sent':
									$controller->send();
									break;
							}
						}
					}
					break;
				case 'profile':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'profile/(\w+)?#', $_SERVER['REQUEST_URI'], $match2))
					{
						$controller = Controller_Profile::getInstance();
						if (empty($match2[1]))
						{
							clearAllCookies();
							session_start();
							if (isset($_SESSION['searchProfilePhone']) and isset($_SESSION['date']) and isset($_SESSION['lastURI']))
							{
								$controller->search($_SESSION['searchProfilePhone'], $_SESSION['lastURI']);
							}
							elseif (isset($_SESSION['selectedCarIndex']) and isset($_SESSION['phone']))
							{
								$controller->selectCars($_SESSION['selectedCarIndex'], $_SESSION['phone']);
							}
							elseif (isset($_SESSION['searchProfilePhone']) and isset($_SESSION['lastURI']))
							{
								$controller->search($_SESSION['searchProfilePhone'], $_SESSION['lastURI']);
							}
							elseif (isset($_SESSION['createdPersonId']))
							{
								$controller->create($_SESSION['createdPersonId']);
							}
							else 
							{
								$controller->index();
							}
						}
						if (isset($match2[1]))
						{
							switch ($match2[1])
							{
								case 'search':
									session_start();
									if (isset($_SESSION['searchProfilePhone']) and isset($_SESSION['lastURI']))
									{
										$_SESSION['date'] = "";
										$controller->search($_SESSION['searchProfilePhone'], $_SESSION['lastURI']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'create':
									session_start();
									if (isset($_SESSION['createdPersonId']))
									{
										$controller->create($_SESSION['createdPersonId']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'car':
									session_start();
									if (isset($_SESSION['selectedCarIndex']) and isset($_SESSION['phone']))
									{
										$controller->selectCars($_SESSION['selectedCarIndex'], $_SESSION['phone']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
							}
						}
					}
					break;
				case 'report':
					if(preg_match('#^'.$_SERVER['HTTP_ROOT'].'report/(\w+)?#', $_SERVER['REQUEST_URI'], $match2))
					{
						$controller = Controller_Report::getInstance();
						if (empty($match2[1]))
						{
							clearAllCookies();
							$controller->index();
						}
						if (isset($match2[1]))
						{
							switch ($match2[1])
							{
								case 'procedureperformed':
									session_start();
									if (isset($_SESSION['startDate']) and isset($_SESSION['endDate']))
									{
										$controller->procedurePerformedReport($_SESSION['startDate'], $_SESSION['endDate']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'inventoryneeded':
									session_start();
									if (isset($_SESSION['startDate']) and isset($_SESSION['endDate']))
									{
										$controller->inventoryNeededReport($_SESSION['startDate'], $_SESSION['endDate']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'inventoryused':
									session_start();
									if (isset($_SESSION['startDate']) and isset($_SESSION['endDate']))
									{
										$controller->inventoryUsedReport($_SESSION['startDate'], $_SESSION['endDate']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
								case 'appointmenthours':
									session_start();
									if (isset($_SESSION['startDate']) and isset($_SESSION['endDate']))
									{
										$controller->appointmentHoursReport($_SESSION['startDate'], $_SESSION['endDate']);
									}
									else
									{
										$controller->index();
									}
									session_write_close();
									break;
							}
						}
					}
					break;
			}
		}
	}
